package com.xiangban.xiangbanmall.springTask;

import com.xiangban.xiangbanmall.dao.UserMapper;
import com.xiangban.xiangbanmall.fegin.UserServiceFeign;
import com.xiangban.xiangbanmall.model.TUser;
import com.xiangban.xiangbanmall.model.res.ApiResult;
import com.xiangban.xiangbanmall.utils.RedisUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;

import static com.xiangban.xiangbanmall.utils.CommonUtils.BizEmpty;

/**
 * @Author Mr. MaYaYun
 * @Data 2021/11/8 16:37
 */
@Component
@EnableScheduling
public class TimedTask {

    @Autowired
    private UserServiceFeign userServiceFeign;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private UserMapper userMapper;

    @Scheduled(initialDelay = 10000,fixedRate = 1000*60*60*24)
    public void task(){
        ApiResult<List<TUser>> userList = userServiceFeign.getUserList();
        String lock = redisUtil.get("run_batch_lock");
        if (BizEmpty(lock) || lock.equals("true")){
            redisUtil.set("run_batch_lock","false",-1);
            if (userList.isSuccess()){
                List<TUser> dataMap = userList.getDataMap();
                if (dataMap != null){
                    for (TUser tUser : dataMap) {
                        TUser tUser1 = userMapper.selectUserInfo(tUser.getUid());
                        if (tUser1 == null){
                            userMapper.insertTUser(tUser);
                        }
                    }
                }
            }
        }
        redisUtil.set("run_batch_lock","true",-1);


    }


}
